﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;

namespace ProjectAutumn.Window
{
    public static class WindowManager
    {
        internal static string GetDll(WindowSystems window)
        {
            string assembly = null;
            switch (window)
            {
                case WindowSystems.SDL:
                    assembly = "ProjectAutumn.WindowSystem.SDL.dll";
                    break;
                case WindowSystems.Form:
                    assembly = "ProjectAutumn.WindowSystem.FormsWindow.dll";
                    break;
                case WindowSystems.GLUT:
                    assembly = "ProjectAutumn.WindowSystem.GLUT.dll";
                    break;
            }
            return assembly;
        }
        public static WindowDevice CreateWindow(WindowSystems window, Game game)
        {
            if (window == WindowSystems.Null) return new NullWindow(game);
            return Utility.LoadClass(GetDll(window), typeof(WindowDevice), game) as WindowDevice;
        }
        public static bool Exists(WindowSystems window)
        {
            if (window == WindowSystems.Null) return true;
            string assembly = GetDll(window);
            if (File.Exists(assembly))
            {
                try
                {
                    Assembly.LoadFile(new FileInfo(assembly).FullName);
                    return true;
                }
                catch (BadImageFormatException)
                {
                    return true;
                }
#if DEBUG
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    Debug.WriteLine(e);
                }
#endif
                catch { }
            }
            return false;
        }
    }
}
